<?php
/**
 * User: hjd-yao
 * Date: 2018/12/17
 * Time: 17:30
 */

namespace Develop\Models;

use App\Models\System\ConfigModel as BaseConfigModel;

class ConfigModel extends BaseConfigModel
{
    public function saveConfig($ipt_data)
    {
        $data['name'] = $ipt_data['config_name'];
        $data['title'] = $ipt_data['config_title'];
        $data['type'] = $ipt_data['config_type'];
        $data['group'] = $ipt_data['config_group'];
        $data['value'] = $ipt_data['config_value'];
        $data['extra'] = $ipt_data['config_extra'];
        $data['remark'] = $ipt_data['remark'];
        $data['status'] = isset($ipt_data['status'])?$ipt_data['status']:0;
        $data['sort'] = isset($ipt_data['sort'])?$ipt_data['sort']:1000;
        $data['created_at'] = time();
        if(self::insert($data)){
            $this->clearConfigCache();
            return true;
        }
        return false;
    }

    public function updateConfig($name,$ipt_data)
    {
        $data['title'] = $ipt_data['config_title'];
        $data['type'] = $ipt_data['config_type'];
        $data['group'] = $ipt_data['config_group'];
        $data['value'] = $ipt_data['config_value'];
        $data['extra'] = $ipt_data['config_extra'];
        $data['remark'] = $ipt_data['remark'];
        $data['status'] = isset($ipt_data['status'])?$ipt_data['status']:0;
        $data['sort'] = isset($ipt_data['sort'])?$ipt_data['sort']:1000;
        $data['updated_at'] = time();
        if(self::where('name',$name)->update($data)){
            $this->clearConfigCache();
            return true;
        }
        return false;
    }

    public function deleteConfig($name)
    {
        if(self::where('name',$name)->delete()){
            $this->clearConfigCache();
            return true;
        }
        return false;
    }

    public function changeStatus($name,$status)
    {
        $data['status'] = $status;
        $data['updated_at'] = time();
        if(self::where('name',$name)->update($data)){
            $this->clearConfigCache();
            return true;
        }
        return false;
    }
}